package com.javafactroy.kunlong.test.jsengine;

import com.javafactroy.kunlong.jsengine.JavascriptEngine;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Calculate 测试文件
 *
 * @author P
 * @date 2018年4月25日
 */


public class TestEngine {
    public static class JsCalParam {
        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        String remark;

    }

    public static class JsCalResult {

        String remark;
        double amount;

        public double getAmount() {
            return amount;
        }

        public void setAmount(double amount) {
            this.amount = amount;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }


    }
    ScriptEngine scriptEngine = JavascriptEngine.getScriptEngine();//scriptManager.getEngineByName("nashorn");        // scriptEngine.eval(code);

    @Before
    public void setUp() throws Exception {
        JavascriptEngine.loadJsFile(scriptEngine, "fun.js");
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test001_JavaScriptExpr() throws ScriptException {
        System.out.println("*******testJavaScriptExpr");
        System.out.println("\n");

        Map<String, Object> params = new HashMap<>();
        params.put("va", 100);
        params.put("vb", 100);
        String expr = "(va * 10 + vb)/10";
        List<String> paramNames = JavascriptEngine.findExprParamNames(expr);
        Object result = JavascriptEngine.doExpr(expr, params);

        System.out.println("*** 执行表达式   " + expr + "  = " + result);
    }

    @Test
    public void test002_JavaScriptCal() throws ScriptException, FileNotFoundException, NoSuchMethodException {
        System.out.println("*******testJavaScriptCal");
        JsCalResult result = new JsCalResult();

        Invocable engine = (Invocable) scriptEngine;
        JsCalParam param = new JsCalParam();
        param.remark = "JavaScriptCal myRemark! ";
        engine.invokeFunction("calFee", param, param, result);
        System.out.println("result = "+result.getAmount());
        System.out.print (result.getRemark());
    }

    @Test
    public void test003_JavaScriptUpper() throws ScriptException, FileNotFoundException, NoSuchMethodException {
        System.out.println("*******testJavaScriptUpper");


        Invocable engine = (Invocable) scriptEngine;
        Object result = engine.invokeFunction("toUpperCase", "leijm");
        System.out.println("leijm==>" + result);
    }
    @Test
    public void test004_JavaScriptMin() throws ScriptException, FileNotFoundException, NoSuchMethodException {
        System.out.println("*******testJavaScriptMin");

        Invocable engine = (Invocable) scriptEngine;
        Object result = engine.invokeFunction("min" );
        System.out.println("min==>" + result);
    }
}
